<?php
declare(strict_types=1);

namespace app\admin\controller;

use app\common\controller\BaseController;
use app\common\model\Language as LanguageModel;
use app\common\utils\JsonResponse;
use think\facade\Db;
use think\facade\Request;

class Language extends BaseController
{
    /**
     * 获取语言列表
     * @permission system:language:getList
     */
    public function getList()
    {
        $params = Request::get();
        
        $query = LanguageModel::order('sort', 'asc');
        
        // 名称搜索
        if (!empty($params['name'])) {
            $query->whereLike('name', "%{$params['name']}%");
        }
        
        // 状态筛选
        if (isset($params['status']) && $params['status'] !== '') {
            $query->where('status', intval($params['status']));
        }
        
        $list = $query->select();
        
        return JsonResponse::success([
            'list' => $list
        ]);
    }
    
    /**
     * 添加语言
     * @permission system:language:doAdd
     */
    public function doAdd()
    {
        $params = Request::post();
        
        Db::startTrans();
        try {
            // 检查语言代码是否已存在
            if (LanguageModel::where('code', $params['code'])->find()) {
                throw new \Exception('语言代码已存在');
            }
            
            // 如果设置为默认语言，则将其他语言的默认状态取消
            if (!empty($params['is_default'])) {
                LanguageModel::where('is_default', 1)->update(['is_default' => 0]);
            }
            
            LanguageModel::create([
                'name' => $params['name'],
                'code' => $params['code'],
                'icon' => $params['icon'] ?? '',
                'sort' => $params['sort'] ?? 0,
                'status' => $params['status'] ?? 1,
                'is_default' => $params['is_default'] ?? 0
            ]);
            
            Db::commit();
            return JsonResponse::success([], '添加成功');
        } catch (\Exception $e) {
            Db::rollback();
            return JsonResponse::error($e->getMessage());
        }
    }
    
    /**
     * 编辑语言
     * @permission system:language:doEdit
     */
    public function doEdit()
    {
        $params = Request::post();
        
        Db::startTrans();
        try {
            $language = LanguageModel::find($params['language_id']);
            if (!$language) {
                throw new \Exception('语言不存在');
            }
            
            // 检查语言代码是否已存在(排除自身)
            if (LanguageModel::where('code', $params['code'])
                ->where('language_id', '<>', $params['language_id'])
                ->find()) {
                throw new \Exception('语言代码已存在');
            }
            
            // 如果设置为默认语言，则将其他语言的默认状态取消
            if (!empty($params['is_default'])) {
                LanguageModel::where('is_default', 1)->update(['is_default' => 0]);
            }
            
            $language->save([
                'name' => $params['name'],
                'code' => $params['code'],
                'icon' => $params['icon'] ?? '',
                'sort' => $params['sort'] ?? 0,
                'status' => $params['status'] ?? 1,
                'is_default' => $params['is_default'] ?? 0
            ]);
            
            Db::commit();
            return JsonResponse::success([], '修改成功');
        } catch (\Exception $e) {
            Db::rollback();
            return JsonResponse::error($e->getMessage());
        }
    }
    
    /**
     * 删除语言
     * @permission system:language:doDelete
     */
    public function doDelete()
    {
        $languageId = Request::post('language_id');
        
        try {
            $language = LanguageModel::find($languageId);
            if (!$language) {
                throw new \Exception('语言不存在');
            }
            
            // 默认语言不能删除
            if ($language->is_default) {
                throw new \Exception('默认语言不能删除');
            }
            
            $language->delete();
            return JsonResponse::success([], '删除成功');
        } catch (\Exception $e) {
            return JsonResponse::error($e->getMessage());
        }
    }
} 